package phi.engine.layers
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	
	import flash.geom.Point;
	
	import phi.engine.actors.ActorOptions;
	import phi.engine.actors.IActor;
	import phi.engine.core.GameTime;
	import phi.engine.utils.PhiMathUtil;

	public class ScrollActorLayer2
	{
		// Protected vars
		public var velocity :b2Vec2 = new b2Vec2( -3, 0 );
		
		// Private vars
		private var _scrollActorClass :Class;
		
		public function ScrollActorLayer2()
		{
		}

		/*public function set scrollActorClass( value:Class ):void
		{
			_scrollActorClass = value;
		}
		
		public function get scrollActorClass():Class
		{
			return _scrollActorClass;
		}
		
		public function get scollActors():Array
		{
			var result :Array = [];
			
			for each( var actor :IActor in actors )
				if( actor.data.hasOwnProperty("scrollable") )
					if( actor.data.scrollable )
						result.push( actor );
				
			return result;
		}
		
		override public function update( gameTime:GameTime ):void
		{
			super.update( gameTime );
			
			destroyUnusedActors();
			createNewActors();
			
			scroll();
		}
		
		protected function destroyUnusedActors():void
		{
		}
		
		protected function createNewActors():void
		{
			var startX :Number = calculateStartX();
			
			while( startX < engine.screenWidth )
			{
				var actor :IActor = new scrollActorClass();
				var actorOptions :ActorOptions = new ActorOptions();
				
				var w :Number = 80 + (500 * Math.random() );
				var h :Number = (130 + (200 * Math.random()))
				
				actorOptions.size = new Point( w, 1);
				actorOptions.startPosition.x = startX + actorOptions.size.x / 2;
				actorOptions.startPosition.y = engine.screenHeight - actorOptions.size.y/2 - h;
				actorOptions.bodyType = b2Body.KINEMATIC_BODY;
				actorOptions.velocity = new b2Vec2( 5, 0 );
				actorOptions.fixRotation = true;
				
				actor.changeOptions( actorOptions );
				actor.data.scrollable = true;
				
				addActor( actor );
				
				startX = calculateStartX();
			}
		}

		protected function calculateStartX():Number
		{
			var result :Number = -5;
			var padding :Number = 80 + ( 500 * Math.random() );
			
			
			for each( var actor :IActor in scollActors )
				result = Math.max( result, actor.position.x + actor.size.x/2 );
				
			padding = 14;
			if( result > 0 )
				result += padding;
			
			return result;
		}
		
		protected function scroll():void
		{
			velocity.x -= 0.02;
			
			for each( var actor :IActor in scollActors )
				actor.body.SetLinearVelocity( velocity );
		}*/
	}
}